#include <Arduino.h>
#include <WString.h>
#include <../lib/U8g2/src/U8g2lib.h>
#include "ziku.h"
#include "../lib/Explode/Explode.h"
int  startPrompt= 18;
int endPrompt = 20;
U8G2_ST7920_128X64_1_SW_SPI lcd(U8G2_R0, /* clock=*/ 12, /* data=*/ 11, /* CS=*/ 10, /* reset=*/ 8);

void setup() {
    Serial.begin(9600);
    pinMode(startPrompt,OUTPUT);
    pinMode(endPrompt,OUTPUT);
    digitalWrite(startPrompt,HIGH);
    digitalWrite(endPrompt,HIGH);
    lcd.begin();
    lcd.enableUTF8Print();
    lcd.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
    lcd.setFontDirection(0);
    lcd.firstPage();
    do {
        lcd.drawXBMP(0, 0, 128, 64, logo);
    } while (lcd.nextPage());
    delay(3000);
}

void loop() {
    delay(1000);
    String data = "";
    while (Serial.available() > 0) {
        data += char(Serial.read());
        delay(5);
    }
    data.trim();
    if (data.compareTo("")!=0){

        if (data.compareTo("startPrompt*n*n*n")==0){
            digitalWrite(startPrompt,LOW);
            delay(1000);
            digitalWrite(startPrompt,HIGH);
            delay(1000);
        }
        if (data.compareTo("endPrompt*n*n*n")==0){
            digitalWrite(endPrompt,LOW);
            delay(1000);
            digitalWrite(endPrompt,HIGH);
            delay(1000);
        }
        lcd.enableUTF8Print();
        lcd.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
        lcd.setFontDirection(0);
        lcd.firstPage();
        do {
            int size;
            char **ret = explode('*',data.c_str() , &size);
            lcd.setCursor(0, 15);
            lcd.print("STATUS:");
            lcd.setCursor(0, 30);
            lcd.print("NUMBER:");
            lcd.setCursor(0, 45);
            lcd.print("SPEED :");
            lcd.setCursor(0, 60);
            lcd.print("TEMP  :");
            lcd.setCursor(lcd.getStrWidth("STATUS:") + 5, 15);
            lcd.print(ret[0]);
            lcd.setCursor(lcd.getStrWidth("NUMBER:") + 5, 30);
            lcd.print(ret[1]);
            lcd.setCursor(lcd.getStrWidth("SPEED :") + 5, 45);
            lcd.print(ret[2]);
            lcd.setCursor(lcd.getStrWidth("TEMP  :") + 5, 60);
            lcd.print(ret[3]);
            freeExplode(ret,size);
        } while (lcd.nextPage());
    }

}